package com.tl.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.tl.domain.Ball;

public class ImportData {

	private static Logger logger = Logger.getLogger(UpdateDataTask.class);
	public static void main(String[] args) {
		List<Ball> balls = ImportData.getNetAllData();
		importDB(balls);
//		getNetAllData();
//		getNetDataByYear();
//		getNetAllDataTest();
//		test2();
	}

	public static void importDB(List<Ball> balls) {
		if(balls==null){
			logger.warn("获取网页数据出现错误!");
			return ;
		}
		if(balls.size()==0){
			logger.info("没有数据更新!");
		}
		int size = 100;
		String sql = "insert into lottery(id,hq1,hq2,hq3,hq4,hq5,hq6,lq1,issue,year,batch) values(null,?,?,?,?,?,?,?,?,?,?)";
		Long startTime = System.currentTimeMillis();
		Connection conn = null;
		PreparedStatement prep = null;
		try {
			conn = JDBCUtils.getConn();
			conn.setAutoCommit(false);
			prep = conn.prepareStatement(sql.toString());
			int count = balls.size() / 100 + 1;
			for (int i = 0; i < count; i++) {
				for (int j = i * size; (j < (i + 1) * size && j < balls.size()); j++) {
					Ball b = balls.get(j);
					for (int k = 1; k <= b.hq.length; k++) {
						prep.setString(k, b.hq[k - 1]);
					}
					prep.setString(7, b.getLq());
					prep.setDate(8, java.sql.Date.valueOf(b.getTime()));
					prep.setString(9, b.getYear());
					prep.setInt(10, b.getBatch());
					prep.addBatch();
				}

				prep.executeBatch();
				conn.commit();
				prep.clearBatch();
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.closeConn(conn, prep, null);
		}
		Long endTime = System.currentTimeMillis();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		logger.info(format.format(new Date())+" 用时:" + (endTime - startTime) + "毫秒;共录入:"+balls.size()+"条数据");
	}
	
	
	/**
	 * 获取 2014年的数据
	 * @return
	 */
	public static List<Ball> getNetData() {
		URL url = null;
		String regex = "<td class=\"td1\">([\\w-]*)</td>|<span class=\"ball_1\">([\\w-]*)</span>|<span class=\"ball_2\">([\\w-]*)</span>";
		String strUrl = "http://hao123.lecai.com/lottery/draw/list/50";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = null;

		List<Ball> balls = new ArrayList<Ball>();

		try {
			// 获取URL地址
			url = new URL(strUrl);
			// 连接URL地址
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			urlConnection.connect();
			// 定义输入流
			InputStream in = urlConnection.getInputStream();
			InputStreamReader rea = new InputStreamReader(in, "utf-8");
			BufferedReader bur = new BufferedReader(rea);
			String len = "";

			Ball b = new Ball();
			int count = 0;

			while ((len = bur.readLine()) != null) {
				matcher = pattern.matcher(len);
				while (matcher.find()) {
					String time = matcher.group(1);
					String hq = matcher.group(2);
					String lq = matcher.group(3);
					if (StringUtils.isNotBlank(time)) {
						b = new Ball();
						b.setTime(time);
						count = 0;
					}
					if (StringUtils.isNotBlank(hq)) {
						b.hq[count] = hq;
						count++;
					}
					if (StringUtils.isNotBlank(lq)) {
						b.setHq(b.hq);
						b.setLq(lq);
						balls.add(b);
						count = 0;
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return balls;
	}
	
	
	/**
	 * 获取指定 URL的数据
	 * @return
	 */
	public static void getNetDataByYear() {
		List<Ball> balls = new ArrayList<Ball>();
		URL url = null;
		String strUrl = "http://hao123.lecai.com/lottery/draw/list/50?d=2005-01-01";
		String regex = "<td class=\"td1\">([\\w-]*)</td>|<span class=\"ball_1\">([\\w-]*)</span>|<span class=\"ball_2\">([\\w-]*)</span>";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = null;

		try {
			// 获取URL地址
			url = new URL(strUrl);
			// 连接URL地址
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.connect();
			// 定义输入流
			InputStream in = urlConnection.getInputStream();
			InputStreamReader rea = new InputStreamReader(in, "utf-8");
			BufferedReader bur = new BufferedReader(rea);
			String len = "";

			Ball b = new Ball();
			int count = 0;

			while ((len = bur.readLine()) != null) {
				matcher = pattern.matcher(len);
				while (matcher.find()) {
					String time = matcher.group(1);
					String hq = matcher.group(2);
					String lq = matcher.group(3);
					if (StringUtils.isNotBlank(time)) {
						b = new Ball();
						b.setTime(time);
						count = 0;
					}
					if (StringUtils.isNotBlank(hq)) {
						b.hq[count] = hq;
						count++;
					}
					if (StringUtils.isNotBlank(lq)) {
						b.setHq(b.hq);
						b.setLq(lq);
						balls.add(b);
						count = 0;
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		for(Ball b : balls){
			System.out.println(b.toString());
		}
		System.out.println("总共条数:"+balls.size());
	}
	
	/**
	 * 获取所有数据
	 * @return
	 */
	public static List<Ball> getNetAllData(){
		List<Ball> balls = new ArrayList<Ball>();
		Map<String,String> urls = getURLs();
		int size = 0;
		for (Map.Entry<String, String> entry : urls.entrySet()) {
			String url = entry.getKey();
			String year = entry.getValue();
			getAnalysisHTML(url,year,balls);
			int index = 1;
			for(int i=balls.size()-1;i>=size;i--){
				Ball b = balls.get(i);
				b.setBatch(index);
				index++;
			}
			System.out.println(year+"年的----"+(balls.size()-size)+"条数据");
			size = balls.size();
		}
		return balls;
	}
	

	/**
	 * 获取所有数据
	 * @return
	 */
	public static void getNetAllDataTest(){
		List<Ball> balls = new ArrayList<Ball>();
		Map<String,String> urls = getURLs();
		int size = 0;
		for (Map.Entry<String, String> entry : urls.entrySet()) {
			String url = entry.getKey();
			String year = entry.getValue();
			getAnalysisHTML(url,year,balls);
			System.out.println(year+"年的----"+(balls.size()-size)+"条数据");
			size = balls.size();
		}
		
//		for (Ball ball : balls) {
//			System.out.println(ball);
//		}
	}
	
	public static void test2(){
		List<Ball> balls = new ArrayList<Ball>();
		
		String[] years = {"2004","2005","2006","2007","2008","2009","2010","2011","2012","2013","2014"};
		String url = "http://hao123.lecai.com/lottery/draw/list/50?d=";
		int size = 0;
		for(String year:years){
			String tURL = url+year+"-01-01";
			getAnalysisHTML(tURL,year,balls);
			System.out.println(year+"年有"+(balls.size()-size)+"条数据");
			size = balls.size();
		}
	}
			
	/**
	 * 获取 所有 URL
	 * @return
	 */
	public static Map<String,String> getURLs(){
		Map<String,String> urls = new LinkedHashMap<String, String>();
		
		//http://hao123.lecai.com/lottery/draw/list/50?d=2014-01-01
		String strURL = "http://hao123.lecai.com/lottery/draw/list/50";
		URL url = null;
		
		// <option value="2013-01-01">2013</option>
		String regex = "<option.*[value=\"]([\\w-]*)\">(\\w*)</option>";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = null;
		
		try {
			url = new URL(strURL);
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.connect();
			InputStream in = urlConnection.getInputStream();
			InputStreamReader rea = new InputStreamReader(in, "utf-8");
			BufferedReader bur = new BufferedReader(rea);
			String len = "";
			while ((len = bur.readLine()) != null) {
				matcher = pattern.matcher(len);
				while (matcher.find()) {
					String key = matcher.group(1);
					String value = matcher.group(2);
					key = strURL+"?d="+key;
					urls.put(key, value);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return urls;
	}
	
	
	/**
	 * 获取页面具体数据
	 * @param netURL
	 * @param year
	 */
	public static void getAnalysisHTML(String strUrl,String year,List<Ball> balls) {

		URL url = null;
		String regex = "<td class=\"td1\">([\\w-]*)</td>|<span class=\"ball_1\">([\\w-]*)</span>|<span class=\"ball_2\">([\\w-]*)</span>";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = null;

		try {
			url = new URL(strUrl);
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.connect();
			InputStream in = urlConnection.getInputStream();
			InputStreamReader rea = new InputStreamReader(in, "utf-8");
			BufferedReader bur = new BufferedReader(rea);
			String len = "";

			Ball b = new Ball();
			int count = 0;
			while ((len = bur.readLine()) != null) {
				matcher = pattern.matcher(len);
				while (matcher.find()) {
					String time = matcher.group(1);
					String hq = matcher.group(2);
					String lq = matcher.group(3);
					if (StringUtils.isNotBlank(time)) {
						b = new Ball();
						b.setTime(time);
						count = 0;
					}
					if (StringUtils.isNotBlank(hq)) {
						b.hq[count] = hq;
						count++;
					}
					if (StringUtils.isNotBlank(lq)) {
						b.setHq(b.hq);
						b.setLq(lq);
						b.setYear(year);
						balls.add(b);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
